class HotfilesController < ApplicationController
  before_filter :auth,:except => [:index]
  def submit
  	begin
  	  if params[:filesizelong].to_i<=0
		@result='paramwrong'
  	  else
		  if (Hotfile.find_by_filehash(params[:filehash]).nil?)  
		  	mF=Hotfile.new
			mF.filehash=params[:filehash]
			mF.description=params[:filedesc]
			mF.name=params[:filename]
			mF.filesizelong=params[:filesizelong].to_i
			mF.filesize=params[:filesize]
			mF.user_id=session[:user_id]
			mF.create_at=Time.now
			mF.status=0
			mF.clicked=0
			mF.save
			@result='ok'
		  else
			@result='fileexists'
		  end
  	  end
	rescue
		@result='error'
	end
	render :layout => false 
  end
 
  def index
  	if session[:admin].nil?
		redirect_to :controller=>'admin'
		return
  	end
  	@actype=params[:actype].to_i
  	if request.post?
		fid=params[:fid].to_i
		ffile=Hotfile.find(fid)
		unless ffile.nil?
		  atype=params[:featured].to_i
		  if (atype==1)||(atype==2)
			ffile.status=atype-1
			ffile.save
		  end
		  if (atype==-1)
		  	ffile.destroy
		  end
		  if (atype==-2)
		  	ffile.description=params[:filedesc]
			ffile.save
		  end		  
		end
  	end
	@page=params[:page].to_i
	if(@actype==1)
	  @count=(Hotfile.count(:all,:conditions=>'status=1')+9)/10
	  @hotfiles=Hotfile.find(:all,:order => "id DESC",:conditions=>'status=1',:limit =>10,:offset=>@page*10)
	else
	  @actype=0
	  @count=(Hotfile.count(:all)+9)/10
	  @hotfiles=Hotfile.find(:all,:order => "id DESC",:limit =>10,:offset=>@page*10)
	end
	  render :layout => false 
  end
  def newest
	  @count=User.count(:all)
	  @page=params[:page].to_i
	  @hotfiles=Hotfile.find(:all,:order => "id DESC",:limit =>10,:offset=>@page*10)
  end
  def newest1
	  @count=User.count(:all)
	  @page=params[:page].to_i
	  @hotfiles=Hotfile.find(:all,:limit =>10,:offset=>@page*10, :conditions=>"status=1")
  end
end
